package cn.bdqn.controller;

import cn.bdqn.pojo.Houseinfo;
import cn.bdqn.pojo.Housetype;
import cn.bdqn.service.HouseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HouseController {
    @Resource
    private HouseService houseService;
    @GetMapping("/sel")
    public ModelAndView sel(){

        List<Houseinfo> list=houseService.sel();
        ModelAndView mv=new ModelAndView();
        mv.addObject("infos",list);
        mv.setViewName("info");
        return mv;
    }
    @PostMapping("/add")
    public ModelAndView add(Houseinfo houseinfo){
        Integer pan= houseService.ins(houseinfo);
        ModelAndView mv=new ModelAndView();
        if(pan>0){
            List<Houseinfo> list=houseService.sel();
            mv.addObject("infos",list);
            mv.setViewName("info");
        }else {
            mv.setViewName("add");
        }
        return mv;
    }
    @PostMapping("/upd")
    public ModelAndView upd(Houseinfo houseinfo){
        Integer pan= houseService.upd(houseinfo);
        ModelAndView mv=new ModelAndView();
        if(pan>0){
            List<Houseinfo> list=houseService.sel();
            mv.addObject("infos",list);
            mv.setViewName("info");
        }else {
            List<Houseinfo> list=houseService.sel();
            mv.addObject("infos",list);
            mv.setViewName("info");
        }
        return mv;
    }
    @GetMapping("/addsel")
    public ModelAndView addsel(Integer houseid){
        ModelAndView mv=new ModelAndView();
        Houseinfo houseinfo=new Houseinfo();
        houseinfo.setTypeid(0);
        if (houseid!=null && houseid>0){
            houseinfo=houseService.selinfo(houseid);

        }
        mv.addObject("houseinfo",houseinfo);
        List<Housetype> hou=houseService.housel();
        mv.addObject("hou",hou);

        if (houseid!=null && houseid>0){
            mv.setViewName("upd");
        }else {
            mv.setViewName("add");
        }
        return mv;
    }
    @GetMapping("/del")
    public ModelAndView del(Integer houseid){
        Integer pan=houseService.del(houseid);
        ModelAndView mv=new ModelAndView();
        if (pan>0){

            List<Houseinfo> list=houseService.sel();
            mv.addObject("infos",list);
            mv.setViewName("info");
        }
        return mv;

    }
    @GetMapping("/selzzzz")
    public ModelAndView selzzzz(Integer typeid){

        List<Houseinfo> list=houseService.houselz(typeid);
        ModelAndView mv=new ModelAndView();

        mv.addObject("infos",list);
        mv.setViewName("info");
        return mv;

    }
}
